package com.smzd.ccms.business;

import java.util.Date;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smzd.ccms.dao.ArticleDao;
import com.smzd.ccms.domain.Article;
import com.smzd.ccms.domain.Image;
import com.smzd.core.dao.IDao;
import com.smzd.framework.core.business.AbstractFGService;
import com.smzd.framework.system.domain.User;
import com.smzd.framework.utils.db.FGRowConverter;
import com.smzd.framework.utils.db.RPage;
import com.smzd.utils.SimpleDateTimeUtil;

@Service
public class ArticleService extends AbstractFGService<Article> {
	private static ArticleService instance;
    @Autowired
    private ArticleDao dao;
    
    @Override
    protected IDao<Article> getDao() {
        return dao;
    }
    
    @Override
    public FGRowConverter<Article> getConverter() {
        return converter;
    }

    private static FGRowConverter<Article> converter = new FGRowConverter<Article>() {
        @Override
        public Object[] convert(Article elem) {
            Date posttime = elem.getPosttime();
            String str_posttime = null;
            if(posttime!=null){
                str_posttime = SimpleDateTimeUtil.formatToDateTime(posttime);
            }
            Date lastmodified = elem.getLastmodified();
            String str_lastmodified = null;
            if(lastmodified!=null){
                str_lastmodified = SimpleDateTimeUtil.formatToDateTime(lastmodified);
            }
            Image image = elem.getImage();
            Object[] obj_image;
            if(image==null){
                obj_image = null;
            }else{
                Integer id_image = image.getId();
                String name_image = image.getName();
                obj_image = new Object[]{id_image, name_image};
            }
            User user = elem.getUser();
            Object[] obj_user;
            if(user==null){
            	obj_user = null;
            }else{
                Integer id_user = user.getId();
                String name_user = user.getName();
                String desc_user = user.getDescription();
                String avatar_user = user.getAvatar();
                obj_user = new Object[]{id_user, name_user, desc_user, avatar_user};
            }
            Object[] fs = new Object[]{
                    elem.getId(),
                    elem.getName(),
                    elem.getDescription(),
                    elem.getSource(),
                    str_posttime,
                    str_lastmodified,
                    obj_image,
                    elem.getSticky(),
                    elem.getStatus(),
                    obj_user
                };
                return fs;
        }
    };

	public Article getFirstArticleByChannelId(int chId) {
		return dao.getFirstArticleByChannelId(chId);
	}

	@PostConstruct
	void init(){
		instance = this;
	}
	public static ArticleService getInstance() {
		return instance;
	}

	
	// 2016-08-18增加根据发布状态来获取文章列表
	/**
	 * 注意，queryType里面放了状态，要根据指定的状态来筛选文章。
	 * @param page
	 * @return
	 */
	public RPage<Article> getArticlesByChId(RPage<Article> page) {
		return dao.getArticlesByChId(page);
	}

	// 2016-08-18增加根据发布状态来获取文章列表
	/**
	 * 注意，queryType里面放了状态，要根据指定的状态来筛选文章。
	 * @param page
	 * @return
	 */
	public RPage<Article> getArticlesByChPath(RPage<Article> page) {
		return dao.getArticlesByChPath(page);
	}

	// 跨越父级获取子栏目中的文章。父栏目的id已经设置到page当中，key为chId
	public RPage<Article> getArticlesByPChId(RPage<Article> page) {
		return dao.getArticlesByPChId(page);
	}

	/**
	 * 获取指定栏目当中的文章，并获取每篇文章的最后一个发言者<br>
	 * 栏目的id放在page.id里面了。
	 * @param page
	 * @return
	 */
    public RPage<Article> getArticlesWithLastComment(RPage<Article> page) {
        return dao.getArticlesWithLastComment(page);
    }

	/**
	 * 在指定的栏目中寻找用户的第一篇文章
	 * 
	 * @param userId
	 * @param channelId
	 * @return
	 */
	public Article getUserFirstArticleWithinChannel(int userId, int channelId) {
		return dao.getUserFirstArticleWithinChannel(userId, channelId);
	}

	/**
	 * 查找用户的文章，在指定的一个或多个栏目中查找。<br>
	 * 用户的ID放在page.id当中；栏目则放在query当中，如果有多个，用逗号隔开。
	 * 
	 * @param page
	 * @return
	 */
	public RPage<Article> getUserArticlesWithinChannels(RPage<Article> page) {
		return dao.getUserArticlesWithinChannels(page);
	}

	public void updateArticleStatusAndScope(Article article){
		dao.updateArticleStatusAndScope(article);
	}
}